﻿using Microsoft.EntityFrameworkCore;

namespace HelenServer.BugEngine.Dal;

public partial class BugEngineDbContext : DbContext
{
    public BugEngineDbContext(DbContextOptions<BugEngineDbContext> options)
        : base(options)
    {

    }

    public virtual DbSet<Browser> Browsers { get; set; } = null!;
    public virtual DbSet<Bug> Bugs { get; set; } = null!;
    public virtual DbSet<BugType> BugTypes { get; set; } = null!;
    public virtual DbSet<Module> Modules { get; set; } = null!;
    public virtual DbSet<OperationLog> OperateLogs { get; set; } = null!;
    public virtual DbSet<OperationLogDetail> OperateLogDetails { get; set; } = null!;
    public virtual DbSet<Os> Os { get; set; } = null!;
    public virtual DbSet<Product> Products { get; set; } = null!;
    public virtual DbSet<Release> Releases { get; set; } = null!;
    public virtual DbSet<Solution> Solutions { get; set; } = null!;

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Bug>(entity =>
        {
            entity.Property(e => e.Id).ValueGeneratedNever();
            entity.Property(m => m.Product).HasConversion<EntityValueConverter<BugEngineDbContext, Product>, EntityValueComparer<Product>>();
            entity.Property(m => m.Module).HasConversion<EntityValueConverter<BugEngineDbContext, Module>, EntityValueComparer<Module>>();
            entity.Property(m => m.Type).HasConversion<EntityValueConverter<BugEngineDbContext, BugType>, EntityValueComparer<BugType>>();
            entity.Property(m => m.Os).HasConversion<EntityValueConverter<BugEngineDbContext, Os>, EntityValueComparer<Os>>();
            entity.Property(m => m.Browser).HasConversion<EntityValueConverter<BugEngineDbContext, Browser>, EntityValueComparer<Browser>>();
            entity.Property(m => m.Cclist).HasConversion<CompositStringToInt32ValueConverter, CompositStringToInt32ValueComparer>();
            entity.Property(m => m.AffectVersion).HasConversion<EntityCollectionValueConverter<BugEngineDbContext, Release>, EntityCollectionValueComparer<Release>>();
            entity.Property(m => m.Resolution).HasConversion<EntityValueConverter<BugEngineDbContext, Solution>, EntityValueComparer<Solution>>();
            entity.Property(m => m.ResolvedVersion).HasConversion<EntityValueConverter<BugEngineDbContext, Release>, EntityValueComparer<Release>>();
            entity.Property(m => m.DuplicateBug).HasConversion<EntityValueConverter<BugEngineDbContext, Bug>, EntityValueComparer<Bug>>();
            entity.Property(m => m.LinkBugs).HasConversion<EntityCollectionValueConverter<BugEngineDbContext, Bug>, EntityCollectionValueComparer<Bug>>();
            entity.Property(m => m.Cases).HasConversion<CompositStringToInt32ValueConverter, CompositStringToInt32ValueComparer>();
            entity.Property(m => m.EditorFiles).HasConversion<CompositStringToInt32ValueConverter, CompositStringToInt32ValueComparer>();
            entity.Property(m => m.Attachments).HasConversion<CompositStringToInt32ValueConverter, CompositStringToInt32ValueComparer>();
            entity.Property(m => m.LogFiles).HasConversion<CompositStringToInt32ValueConverter, CompositStringToInt32ValueComparer>();
            entity.Property(m => m.OperationLogs).HasConversion<EntityCollectionValueConverter<BugEngineDbContext, OperationLog>, EntityCollectionValueComparer<OperationLog>>();
        });

        modelBuilder.Entity<OperationLog>(entity =>
        {
            entity.Property(e => e.Details).HasConversion<EntityCollectionValueConverter<BugEngineDbContext, OperationLogDetail>, EntityCollectionValueComparer<OperationLogDetail>>();
        });

        modelBuilder.Entity<Release>(entity =>
        {
            entity.Property(m => m.Product).HasConversion<EntityValueConverter<BugEngineDbContext, Product>, EntityValueComparer<Product>>();
        });

        modelBuilder.Entity<Module>(entity =>
        {
            entity.Property(m => m.Product).HasConversion<EntityValueConverter<BugEngineDbContext, Product>, EntityValueComparer<Product>>();
            entity.Property(m => m.Parent).HasConversion<EntityValueConverter<BugEngineDbContext, Product>, EntityValueComparer<Product>>();
        });

        this.OnModelCreatingPartial(modelBuilder);
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}